package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class workspaceServiceImpl implements WorkspaceService {

    private final WorkspaceMapper workspaceMapper;

//    @Override
//    public BusinessDataVO businessData() {
//        BusinessDataVO businessDataVO = new BusinessDataVO();
//        LocalDate now = LocalDate.now();
//        List<Map<String,Object>> mapList = workspaceMapper.businessData1(now);
//
//        List<Integer> sumAmount = mapList.stream()
//                .map(item -> (BigDecimal) item.get("sum_amount"))
//                .map(BigDecimal::intValue).collect(Collectors.toList());
//        Optional<Integer> optionalSumAmountAll = sumAmount.stream().reduce(Integer::sum);
//        Integer turnover = 0;
//        if (optionalSumAmountAll.isPresent()){
//            turnover = optionalSumAmountAll.get();
//        }
//
//        Optional<BigDecimal> optionalSumAmountAll = sumAmount.stream().reduce(BigDecimal::add);
//        //营业额
//        BigDecimal turnover = BigDecimal.valueOf(0);
//        if (optionalSumAmountAll.isPresent()){
//            turnover = optionalSumAmountAll.get();
//        }
//
//        List<Long> countAll = mapList.stream()
//                .map(item -> (Long) item.get("count_all"))
//                .collect(Collectors.toList());
//        Optional<Long> optionalCountAll = countAll.stream().reduce(Long::sum);
//        //订单总数
//        Long allOrderCount = 0L;
//        if (optionalCountAll.isPresent()){
//            allOrderCount = optionalCountAll.get();
//        }
//
//        List<Long> countValid = mapList.stream()
//                .map(item -> (Long) item.get("count_valid"))
//                .collect(Collectors.toList());
//        Optional<Long> optionalCountValid = countValid.stream().reduce(Long::sum);
//        //有效订单数
//        Long validOrderCount = 0L;
//        if (optionalCountValid.isPresent()){
//            validOrderCount = optionalCountAll.get();
//        }
//
//        //订单完成率
//        Double orderCompletionRate = validOrderCount.doubleValue() / allOrderCount.doubleValue();
//
//        List<Long> countDay = mapList.stream()
//                .map(item -> (Long) item.get("count_day"))
//                .collect(Collectors.toList());
//        //新增用户数
//        Optional<Long> optionalCountDayLong = countDay.stream().reduce(Long::sum);
//        Long aCountDayLong = 0L;
//        if (optionalCountDayLong.isPresent()){
//            aCountDayLong = optionalCountDayLong.get();
//        }
//
//        Double unitPrice = turnover.doubleValue() / validOrderCount .doubleValue();
//
//        businessDataVO.setTurnover(turnover.doubleValue());
//        businessDataVO.setValidOrderCount(validOrderCount.intValue());
//        businessDataVO.setOrderCompletionRate(orderCompletionRate);
//        businessDataVO.setUnitPrice(unitPrice);
//        businessDataVO.setNewUsers(aCountDayLong.intValue());
//        return businessDataVO;
//    }

    @Override
    public BusinessDataVO businessData() {
        return workspaceMapper.businessData();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
//        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
//        List<Map<String,Object>> mapList = workspaceMapper.overviewOrders();
//        //todo 全部订单
//        Integer countAll = workspaceMapper.selectAllCount();
//        Optional<Long> OptionalAllOrders = mapList.stream().map(item -> (Long) item.get("all_orders")).reduce(Long::sum);
//        Long allOrders = 0L;
//        if (OptionalAllOrders.isPresent()){
//            allOrders = OptionalAllOrders.get();
//        }
//        //todo 已取消订单 6
//        Optional<Long> OptionalCancelOrders = mapList.stream().map(item -> (Long) item.get(6)).reduce(Long::sum);
//        Long cancelOrders = 0L;
//        if (OptionalCancelOrders.isPresent()){
//            cancelOrders = OptionalCancelOrders.get();
//        }
//        //todo 已完成订单 5
//        Optional<Long> OptionalCompleteOrders = mapList.stream().map(item -> (Long) item.get(5)).reduce(Long::sum);
//        Long completeOrders = 0L;
//        if (OptionalCompleteOrders.isPresent()){
//            completeOrders = OptionalCompleteOrders.get();
//        }
//        //todo 待派送订单 3
//        Optional<Long> OptionalDeliveredOrders = mapList.stream().map(item -> (Long) item.get(3)).reduce(Long::sum);
//        Long deliveredOrders = 0L;
//        if (OptionalDeliveredOrders.isPresent()){
//            deliveredOrders = OptionalDeliveredOrders.get();
//        }
//        //todo 待接单订单 2
//        Optional<Long> OptionalWaitingOrders = mapList.stream().map(item -> (Long) item.get(2)).reduce(Long::sum);
//        Long waitingOrders = 0L;
//        if (OptionalWaitingOrders.isPresent()){
//            waitingOrders = OptionalWaitingOrders.get();
//        }
//
//        orderOverViewVO.setWaitingOrders(waitingOrders.intValue());
//        orderOverViewVO.setDeliveredOrders(deliveredOrders.intValue());
//        orderOverViewVO.setCompletedOrders(completeOrders.intValue());
//        orderOverViewVO.setCancelledOrders(cancelOrders.intValue());
//        orderOverViewVO.setAllOrders(countAll);
//        return orderOverViewVO;
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setWaitingOrders(workspaceMapper.selectTodayCountByStatus(Orders.TO_BE_CONFIRMED));
        orderOverViewVO.setDeliveredOrders(workspaceMapper.selectTodayCountByStatus(Orders.CONFIRMED));
        orderOverViewVO.setCompletedOrders(workspaceMapper.selectTodayCountByStatus(Orders.COMPLETED));
        orderOverViewVO.setCancelledOrders(workspaceMapper.selectTodayCountByStatus(Orders.CANCELLED));
        orderOverViewVO.setAllOrders(workspaceMapper.selectTodayCountByStatus(null));
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
//        DishOverViewVO dishOverViewVO = new DishOverViewVO();
//        List<Map<String , Object>> mapList = workspaceMapper.overviewDishes();
//
//        Optional<Long> OptionalSoldLong = mapList.stream().map(item -> (Long) item.get(1)).reduce(Long::sum);
//        Long sold = 0L;
//        if (OptionalSoldLong.isPresent()){
//            sold = OptionalSoldLong.get();
//        }
//
//        Optional<Long> OptionalDisSoldLong = mapList.stream().map(item -> (Long) item.get(0)).reduce(Long::sum);
//        Long disSold = 0L;
//        if (OptionalDisSoldLong.isPresent()){
//            disSold = OptionalDisSoldLong.get();
//        }
//
//        dishOverViewVO.setSold(sold.intValue());
//        dishOverViewVO.setDiscontinued(disSold.intValue());
//        return dishOverViewVO;
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(workspaceMapper.selectCountByStatus(StatusConstant.ENABLE));
        dishOverViewVO.setDiscontinued(workspaceMapper.selectCountByStatus(StatusConstant.DISABLE));
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(workspaceMapper.selectSetmealCountByStatus(StatusConstant.ENABLE));
        setmealOverViewVO.setDiscontinued(workspaceMapper.selectSetmealCountByStatus(StatusConstant.DISABLE));
        return setmealOverViewVO;
    }

}
